package com.ljs.myspringboot.controller;

import com.ljs.myspringboot.entity.Customer;
import com.ljs.myspringboot.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/thymeleaf")
public class HellowthymeleafHaddler {
    @Autowired
    CustomerService customerService;

    @RequestMapping("/html")
    public String html(Model model, HttpServletRequest request) {
        String name = "小刘";
        model.addAttribute("name", name);
        model.addAttribute("sex", "男");
        List<Customer> customerList = customerService.selCus();
        System.out.println("货物单" + customerList);
        model.addAttribute("customerList", customerList);
        request.setAttribute("note", "哈哈哈最棒了");
        System.out.println(new Date());
        request.setAttribute("time", new Date());

        Map<String, Customer> map = new HashMap<>();
        Customer c1 = new Customer(1, "小刘");
        Customer c2 = new Customer(2, "小红");
        Customer c3 = new Customer(3, "小华");
        Customer c4 = new Customer(4, "小明");
        map.put("c1", c1);
        map.put("c2", c2);
        map.put("c3", c3);
        map.put("c4", c4);
        model.addAttribute("map", map);/*把map传过去*/
        return "test";
    }

    @RequestMapping("/form")
    @ResponseBody

    public ModelAndView form(Customer customer) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(customer);
        modelAndView.setViewName("form");
        return modelAndView;
    }

}
